<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="add"> 批量审核通过 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量审核不通过 </button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId2" lay-filter="currentTableFilter2"></table>

        <script type="text/html" id="currentTableBar2">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="reviewed">通过</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-upload" lay-event="unreviewed">不通过</a>
<!--            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="preview">查看</a>-->
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId2',
            url: BasePath+'api/Album/Comments/Unreviewed',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {title: '操作', width: 220, toolbar: '#currentTableBar2', align: "center"},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'albumTitle', width: 120, title: '相册标题'},
                {field: 'content', width: 320, title: '评论内容'},
                {field: 'commentatorName', width: 120, title: '创建者账号'},
                {field: 'commentatorFullName', width: 120, title: '创建者姓名'},
                // {field: 'singns', title: '标签', minWidth: 150},
                // {field: 'albums', title: '相册数', minWidth: 150},
                // {field: 'views', width: 135, title: '点击量', sort: true}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        //相册评论审核通过
        function acceptAlbum(commentId){
            var result=false;
            jq1111.ajax({
                type: "POST",
                crossDomain:true,
                async:false,
                url: BasePath+"api/Album/Comments/"+commentId+"/MarkReviewed",
                contentType: "application/json",
                dataType:"json",
                data:JSON.stringify({

                }),
                xhrFields: {
                    withCredentials: true
                },
                success:function (message) {
                    result=true;
                },
                error:function (message) {
                    console.log(message.responseJSON);
                    result=false;
                },
            });

            return result;
        }

        function deletaComment(id){
            var result=false;
            jq1111.ajax({
                type: "POST",
                crossDomain:true,
                async:false,
                url: BasePath+"api/Album/DeleteComment?commentId="+id,
                contentType: "application/json",
                dataType:"json",
                data:JSON.stringify({

                }),
                xhrFields: {
                    withCredentials: true
                },
                success:function (message) {
                    result=true;
                },
                error:function (message) {
                    console.log(message.responseJSON);
                    result=false;
                },
            });

            return result;
        }

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId2', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter2)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var checkStatus = table.checkStatus('currentTableId2')
                    , data = checkStatus.data;
                if(data.length===0){
                    layer.alert('未选择数据', {
                        title: '操作不合法'
                    });
                    return ;
                }
                var deletedItemCount=0;
                data.forEach(function (deleteData){
                    if(acceptAlbum(deleteData.id)){
                        deletedItemCount++;
                    }
                });
                //TODO 有计数bug，全部显示失败实际成功
                layer.alert('成功'+deletedItemCount+'个数据,'+(data.length-deletedItemCount)+'个失败，如果存在失败，请尝试刷新界面重试', {
                    title: '操作结果'
                });
                table.reload('currentTableId2',{}, 'data');
                console.log(data)
            } else if(obj.event === 'delete'){ // 设置删除操作
                var checkStatus = table.checkStatus('currentTableId2')
                    , data = checkStatus.data;
                if(data.length===0){
                    layer.alert('未选择数据', {
                        title: '操作不合法'
                    });
                    return ;
                }
                var deletedItemCount=0;
                data.forEach(function (deleteData){
                    if(deletaComment(deleteData.id)){
                        deletedItemCount++;
                    }
                });
                //TODO 有计数bug，全部显示失败实际成功
                layer.alert('成功删除'+deletedItemCount+'个数据,'+(data.length-deletedItemCount)+'条删除失败，如果存在删除失败，请尝试刷新界面重试', {
                    title: '操作结果'
                });
                table.reload('currentTableId2',{}, 'data');
                console.log(data)
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter2)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter2)', function (obj) {
            var data = obj.data;
            //编辑相册
            if (obj.event === 'reviewed') {
                if(acceptAlbum(data.id)){
                    layer.alert('操作成功', {
                        title: '操作结果'
                    });
                    table.reload('currentTableId2',{}, 'data');
                }
                else {
                    layer.alert('操作失败', {
                        title: '操作结果'
                    });
                    table.reload('currentTableId2',{}, 'data');
                }
                return false;
            } else if (obj.event === 'unreviewed') {
                if(deletaComment(data.id)){
                    layer.alert('操作成功', {
                        title: '操作结果'
                    });
                    table.reload('currentTableId2',{}, 'data');
                }
                else {
                    layer.alert('操作失败', {
                        title: '操作结果'
                    });
                    table.reload('currentTableId2',{}, 'data');
                }
                return false;
            }else if(obj.event === 'preview'){ // 设置删除操作
                window.location = 'page/new-show.html?albumId='+data.id;
            }
        });

    });
</script>